package com.zhuiyi.yiqingbackend.controller;

import com.zhuiyi.yiqingbackend.common.BaseResponse;
import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.common.ResultUtils;
import com.zhuiyi.yiqingbackend.domain.request.MasterAddRequest;
import com.zhuiyi.yiqingbackend.domain.request.MasterUpdateRequest;
import com.zhuiyi.yiqingbackend.domain.request.MasterUpdateStatuRequest;
import com.zhuiyi.yiqingbackend.domain.response.MasterResponse;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.service.BaseService;
import com.zhuiyi.yiqingbackend.service.MasterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 事件   控制器
 */
@RestController
@RequestMapping("/event")
@Api(value = "event", tags = "事件表的增删改查")
@CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
public class MasterController {

    @Resource
    private MasterService masterService;
    @Resource
    private BaseService baseService;

    /**
     * 获取所有 event api
     * @return
     *
     */
    @GetMapping("/getall")
    @ApiOperation(value = "all", notes = "获取所有event")
    public BaseResponse<List<MasterResponse>> getall() {
        List<MasterResponse> masters = masterService.getMasters();
        return ResultUtils.success(masters);
    }

    /**
     * 添加 event api
     * @return
     *
     */
    @PostMapping("/add")
    @ApiOperation(value = "add", notes = "添加event")
    public BaseResponse add(@RequestBody MasterAddRequest request) {
//        String phone = request.getPhone();
//        if (StringUtils.isBlank(phone)) {
//            throw new BusinessException(ErrorCode.PARAM_ERROR, "电话号码不能为空");
//        }

        boolean result = masterService.addMaster(request);
//        if (!result) {
//            throw new BusinessException(ErrorCode.FAIL, "添加失败");
//        }
        return ResultUtils.success("添加成功");
    }

    /**
     * 删除 event api
     * @return
     *
     */
    @GetMapping("/delete/{id}")
    @ApiOperation(value = "delete", notes = "删除event")
    public BaseResponse delete(@PathVariable int id){

        boolean result = masterService.deleteMasterById(id);
        if (!result) {
            throw new BusinessException(ErrorCode.FAIL, "删除失败");
        }
        return ResultUtils.success("删除成功");
    }

    /**
     * 根据id 获取event详情
     */
    @GetMapping("/getById/{id}")
    @ApiOperation(value = "getById", notes = "根据id获取event详情")
    public BaseResponse<List<MasterResponse>> getById(@PathVariable int id) {
        String gid = Integer.toString(id);
        if (StringUtils.isBlank(gid)) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "id不能为空");
        }
        MasterResponse master = masterService.getMasterById(id);
        if (master == null) {
            throw new BusinessException(ErrorCode.NO_DATA);
        }
        List<MasterResponse> list = new ArrayList();
        list.add(master);
        return ResultUtils.success(list);
    }

    /**
     * 更新 event api
     *
     */
    @PostMapping("/update")
    @ApiOperation(value = "update", notes = "更新event")

    public BaseResponse<Boolean> update(@RequestBody MasterUpdateRequest request) {
        return ResultUtils.success(masterService.updateMaster(request));
    }

    /**
     * 更新 event 状态 api
     */
    @PostMapping("/updateStatus")
    @ApiOperation(value = "updateStatus", notes = "更新event状态")
    public BaseResponse<Boolean> updateStatus(@RequestBody MasterUpdateStatuRequest request) {
//        System.out.println(request.getId()+" "+ request.getEstatus());
        return ResultUtils.success(masterService.updateMasterStatus(request));
    }


}
